package com.xiaoyao.novel.action.page;

import java.util.ArrayList;
import java.util.List;

import com.xiaoyao.novel.common.Constant;
import com.xiaoyao.novel.common.Pagin;
import com.xiaoyao.novel.pojo.Book;
import com.xiaoyao.novel.pojo.Category;
import com.xiaoyao.novel.pojo.FriendLink;
import com.xiaoyao.novel.pojo.WebInfo;
import com.xiaoyao.novel.service.BookService;
import com.xiaoyao.novel.service.CategoryService;
import com.xiaoyao.novel.service.ChapterService;
import com.xiaoyao.novel.service.FriendLinkService;
import com.xiaoyao.novel.service.WebInfoService;

public class NovelAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	protected WebInfoService webInfoService = new WebInfoService();
	protected FriendLinkService friendLinkService = new FriendLinkService();
	protected CategoryService categoryService = new CategoryService();
	protected BookService bookService = new BookService();
	protected ChapterService chapterService = new ChapterService();

	// 获取配置,标题,关键字,描述
	public void getConfig() {
		WebInfo webInfo = webInfoService.getStaticWebInfo();
		request.setAttribute("webInfo", webInfo);
	}

	// 尾部.友情链接
	public void getFooter() {
		List<FriendLink> list = friendLinkService.getAll();
		request.setAttribute("linkList", list);
	}

	// 小说分类
	public List<Category>  listCategory() {
		List<Category> list = categoryService.getAll();
		request.setAttribute("categoryList", list);
		return list;
	}

	// /////////////////////////////////////////////////////////////

	static List<Book> recommendList = new ArrayList<Book>();
	static List<Book> recommendIndexList = new ArrayList<Book>();
	static List<Book> newList = new ArrayList<Book>();
	static List<Book> hotList = new ArrayList<Book>();
	static List<Book> superList = new ArrayList<Book>();
	static long lastUpdate_R = 0;
	static long lastUpdate_N = 0;
	static long lastUpdate_H = 0;
	static long lastUpdate_S  = 0;
	static long INTERVAL_UPDATE = 10 * Constant.MIN; 

	// 图片列表
	public List<Book> getSuper(int size) {
		long now = System.currentTimeMillis();
		if (now - lastUpdate_S > INTERVAL_UPDATE || superList.size() == 0) {
			Pagin pagin = new Pagin();
			pagin.setPageSize(size);
			pagin.getSearchParam().put("orderBy", " isRecommend desc");
			superList = bookService.search(pagin);
			lastUpdate_S = now;
		}
		request.setAttribute("superList", superList);
		return superList;
	}

	// 热门点击列表
	public void getHot(int size) {
		long now = System.currentTimeMillis();
		if (now - lastUpdate_H > INTERVAL_UPDATE || hotList.size() == 0) {
			Pagin pagin = new Pagin();
			pagin.setPageSize(size);
			pagin.getSearchParam().put("orderBy", " clickCount desc");
			hotList = bookService.search(pagin);
			lastUpdate_H = now;
		}
		request.setAttribute("hotList", hotList);
	}
	// 推荐列表
	public void getRecommendIndex(int size, int start) {
		long now = System.currentTimeMillis();
		if (now - lastUpdate_R > INTERVAL_UPDATE || recommendIndexList.size() == 0) {
			Pagin pagin = new Pagin();
			pagin.setPageSize(size);
			pagin.setStart(start);
			pagin.getSearchParam().put("orderBy", " isRecommend desc, clickCount desc ");
			recommendIndexList = bookService.search(pagin);
			lastUpdate_R = now;
		}
		request.setAttribute("recommendIndexList", recommendIndexList);
	}

	// 推荐列表
	public void getRecommend(int size, int start) {
		long now = System.currentTimeMillis();
		if (now - lastUpdate_R > INTERVAL_UPDATE || recommendList.size() == 0) {
			Pagin pagin = new Pagin();
			pagin.setPageSize(size);
			pagin.setStart(start);
			pagin.getSearchParam().put("orderBy", " isRecommend desc, clickCount desc ");
			recommendList = bookService.search(pagin);
			lastUpdate_R = now;
		}
		request.setAttribute("recommendList", recommendList);
	}

	// 新书列表
	public void getNew(int size) {
		long now = System.currentTimeMillis();
		if (now - lastUpdate_N > INTERVAL_UPDATE || newList.size() == 0) {
			Pagin pagin = new Pagin();
			pagin.setPageSize(size);
			pagin.getSearchParam().put("orderBy", " updateTime desc");
			newList = bookService.search(pagin);
			lastUpdate_N = now;
		}
		request.setAttribute("newList", newList);
	}

}
